package org.cuatrovientos.erp.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.cuatrovientos.erp.business.MessageBOInterface;
import org.cuatrovientos.erp.model.Message;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Controller class form message management
 * 
 * @author VB
 */
@Controller
public class MessageController {

	// Get log4j logger
	private static final Logger logger = Logger.getLogger(MessageController.class);

	@Autowired
	private MessageBOInterface messageBO;

	/**
	 * default constructor
	 */
	public MessageController() {
		logger.info("message BEAN instantiated");
	}

	/**
	 * handles default /message or /message/list request
	 * 
	 * @param model
	 * @return the name of the view to show
	 * @RequestMapping({"/message","/message/list" )
	 */
	@RequestMapping({ "/message", "/message/list" })
	public ModelAndView messageDefault() {
		logger.info("default");

		Map<String, Object> model = new HashMap<String, Object>();

		List<Message> messageList = messageBO.selectAll();
		model.put("message", messageList);
		return new ModelAndView("messageList", model);
	}

	/**
	 * handles /message/search request, redirected to default page
	 * 
	 * @param model
	 * @return the name of the view to show
	 * @RequestMapping({"/message/search" )
	 */
	@RequestMapping(method = RequestMethod.POST, value = { "/message/search" })
	public ModelAndView search(@RequestParam(value = "title", defaultValue = "", required = true) String title) {
		logger.info("search");

		Map<String, Object> model = new HashMap<String, Object>();

		List<Message> messageList = messageBO.search(title);
		model.put("message", messageList);

		return new ModelAndView("messageList", model);
	}

	/**
	 * handles delete message, and redirect to message default
	 * 
	 * @param model
	 * @return the name of the view to show
	 * @RequestMapping({"/message","/message/list" )
	 */
	@RequestMapping(method = RequestMethod.GET, value = { "/message/delete" })
	public String deletemessage(@RequestParam(value = "id", defaultValue = "", required = true) int id) {
		logger.info("delete " + id);

		messageBO.deleteById(id);
		return "redirect:/message";
	}

	/**
	 * handles new message request. In the jsp we are using spring's form:form
	 * tags Spring expects a model called by default 'command' or any name we
	 * set to it: in this case "message"
	 * 
	 * @return the name of the view to show
	 * @RequestMapping({"/message/new" )
	 */
	@RequestMapping(method = RequestMethod.GET, value = { "/message/new" })
	public ModelAndView newmessage() {
		logger.info("new message ");
		Message message = new Message();

		return new ModelAndView("Newmessage", "message", message);
	}

	/**
	 * handles new message request. In the jsp we are using spring's form:form
	 * tags Spring expects a model called by default 'command' or any name we
	 * set to it.
	 * 
	 * @return the name of the view to show
	 * @RequestMapping({"/message/new" )
	 */
	@RequestMapping(method = RequestMethod.POST, value = { "/message/save" })
	public String newmessage(@Valid Message message,BindingResult bindingResult) {
		logger.info("new message " + message.toString());

		// In case of validation errors, go back to form
		if (bindingResult.hasErrors()) {
			Map<String, Object> model = new HashMap<String, Object>();
			model.put("message", message);
			return "Newmessage";
		}

		// If data is ok, insert and go on
		messageBO.insert(message);
		return "redirect:/message";
	}

	/**
	 * handles delete message, and redirect to message default
	 * 
	 * @param model
	 * @return the name of the view to show
	 * @RequestMapping({"/message","/message/list" )
	 */
	@RequestMapping(method = RequestMethod.GET, value = { "/message/detail" })
	public ModelAndView messageDetail(@RequestParam(value = "id", defaultValue = "", required = true) int id) {
		logger.info("Detail " + id);
		Map<String, Object> model = new HashMap<String, Object>();
		Message message = messageBO.selectById(id);
		model.put("message", message);

		return new ModelAndView("messageDetail", model);
	}

	/**
	 * handles message update, first part: register loaded
	 * 
	 * @param model
	 * @return the name of the view to show
	 * @RequestMapping({"/message/update" )
	 */
	@RequestMapping(method = RequestMethod.GET, value = { "/message/update" })
	public ModelAndView update(@RequestParam(value = "id", defaultValue = "", required = true) int id) {
		logger.info("update " + id);
		Map<String, Object> model = new HashMap<String, Object>();
		Message message = messageBO.selectById(id);
		model.put("message", message);
		return new ModelAndView("Updatemessage", model);

	}

	/**
	 * handles save update message request. In the jsp we are using spring's
	 * form:form tags Spring expects a model called by default 'command' or any
	 * name we set to it.
	 * 
	 * @return the name of the view to show
	 * @RequestMapping({"/message/saveUpdate" )
	 */
	@RequestMapping(method = RequestMethod.POST, value = { "/message/saveupdate" })
	public String saveUpdate(@Valid Message message,BindingResult bindingResult) {
		logger.info("update message " + message.toString());

		// In case of validation errors, go back to form
		if (bindingResult.hasErrors()) {
			Map<String, Object> model = new HashMap<String, Object>();
			model.put("message", message);
			return "Updatemessage";
		}

		messageBO.update(message);
		return "redirect:/message";
	}
}
